/*     */ package com.zimbra.cs.imap;
/*     */ 
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import java.util.HashSet;
/*     */ import java.util.Iterator;
/*     */ import java.util.LinkedHashMap;
/*     */ import java.util.Map;
/*     */ import java.util.Map.Entry;
/*     */ import java.util.Set;
/*     */ import net.sf.ehcache.CacheException;
/*     */ import net.sf.ehcache.CacheManager;
/*     */ import net.sf.ehcache.Ehcache;
/*     */ import net.sf.ehcache.Element;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ final class EhcacheImapCache
/*     */   implements ImapSessionManager.Cache
/*     */ {
/*     */   private final Ehcache ehcache;
/*     */   private final boolean active;
/*     */   private Map<String, Long> activeCacheUpdateTimes;
/*     */   private static final int ACTIVE_CACHE_THRESHOLD = 1000;
/*     */   
/*     */   EhcacheImapCache(String name, boolean active)
/*     */   {
/*  48 */     this.ehcache = CacheManager.getInstance().getEhcache(name);
/*  49 */     this.active = active;
/*  50 */     if (active) {
/*  51 */       this.activeCacheUpdateTimes = new LinkedHashMap(1000, 0.75F, true) {
/*     */         private boolean isExpired(long timestamp) {
/*  53 */           return timestamp < System.currentTimeMillis() - (LC.imap_authenticated_max_idle_time.intValue() * 1000L + 300000L);
/*     */         }
/*     */         
/*     */         private boolean removeIfExpired(Map.Entry<String, Long> entry) {
/*  57 */           if (isExpired(((Long)entry.getValue()).longValue())) {
/*  58 */             doRemove((String)entry.getKey());
/*  59 */             return true;
/*     */           }
/*  61 */           return false;
/*     */         }
/*     */         
/*     */         private void doRemove(String key)
/*     */         {
/*  66 */           ZimbraLog.imap.debug("removing expired active cache element %s", new Object[] { key });
/*  67 */           EhcacheImapCache.this.ehcache.remove(key);
/*  68 */           remove(key);
/*     */         }
/*     */         
/*     */         protected boolean removeEldestEntry(Map.Entry<String, Long> eldest)
/*     */         {
/*  73 */           if (removeIfExpired(eldest)) {
/*  74 */             Set<String> keysToRemove = new HashSet();
/*  75 */             if (size() > 1000)
/*     */             {
/*  77 */               for (Iterator<Map.Entry<String, Long>> it = entrySet().iterator(); it.hasNext();) {
/*  78 */                 Map.Entry<String, Long> entry = (Map.Entry)it.next();
/*  79 */                 if (!isExpired(((Long)entry.getValue()).longValue())) break;
/*  80 */                 keysToRemove.add(entry.getKey());
/*     */               }
/*     */               
/*     */ 
/*     */ 
/*  85 */               for (String key : keysToRemove) {
/*  86 */                 doRemove(key);
/*     */               }
/*     */             }
/*     */           }
/*  90 */           return false;
/*     */         }
/*     */       };
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   public void put(String key, ImapFolder folder)
/*     */   {
/*  99 */     if (this.active) {
/* 100 */       synchronized (this.activeCacheUpdateTimes) {
/* 101 */         if (!this.ehcache.isKeyInCache(key)) {
/* 102 */           this.ehcache.put(new Element(key, folder));
/* 103 */           ZimbraLog.imap.debug("put key %s", new Object[] { key });
/*     */         }
/* 105 */         long currentTime = System.currentTimeMillis();
/* 106 */         this.activeCacheUpdateTimes.put(key, Long.valueOf(currentTime));
/*     */       }
/*     */       
/* 109 */     } else if (!this.ehcache.isKeyInCache(key)) {
/* 110 */       this.ehcache.put(new Element(key, folder));
/*     */     }
/*     */   }
/*     */   
/*     */   public ImapFolder get(String key)
/*     */   {
/*     */     try
/*     */     {
/* 118 */       Element el = null;
/* 119 */       if (this.active) {
/* 120 */         synchronized (this.activeCacheUpdateTimes) {
/* 121 */           el = this.ehcache.get(key);
/* 122 */           if (el != null) {
/* 123 */             this.activeCacheUpdateTimes.put(key, Long.valueOf(System.currentTimeMillis()));
/* 124 */             ZimbraLog.imap.debug("got Element for key %s", new Object[] { key });
/*     */           } else {
/* 126 */             ZimbraLog.imap.debug("null get for key %s", new Object[] { key });
/*     */           }
/*     */         }
/*     */       } else {
/* 130 */         el = this.ehcache.get(key);
/*     */       }
/* 132 */       return el != null ? (ImapFolder)el.getValue() : null;
/*     */     } catch (CacheException ce) {
/* 134 */       ZimbraLog.imap.error("IMAP cache exception - removing offending key", ce);
/* 135 */       remove(key, true); }
/* 136 */     return null;
/*     */   }
/*     */   
/*     */   private void remove(String key, boolean quiet)
/*     */   {
/*     */     try {
/* 142 */       if (this.active) {
/* 143 */         synchronized (this.activeCacheUpdateTimes) {
/* 144 */           ZimbraLog.imap.debug("removing key %s", new Object[] { key });
/* 145 */           this.activeCacheUpdateTimes.remove(key);
/*     */         }
/*     */       }
/* 148 */       this.ehcache.remove(key);
/*     */     } catch (CacheException ce) {
/* 150 */       if (!quiet) {
/* 151 */         ZimbraLog.imap.error("IMAP cache exception", ce);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public void remove(String key)
/*     */   {
/* 158 */     remove(key, false);
/*     */   }
/*     */   
/*     */   public void updateAccessTime(String key)
/*     */   {
/* 163 */     if (this.active) {
/* 164 */       synchronized (this.activeCacheUpdateTimes) {
/* 165 */         if (this.activeCacheUpdateTimes.containsKey(key)) {
/* 166 */           this.activeCacheUpdateTimes.put(key, Long.valueOf(System.currentTimeMillis()));
/*     */         } else {
/* 168 */           ZimbraLog.imap.warn("active cache needed update but not found: %s", new Object[] { key });
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/imap/EhcacheImapCache.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */